package com.chuanyang.driverh.mainmy.fragment;


import android.Manifest;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseFragment;
import com.chuanyang.driverh.bean.IdentifyCarBean;
import com.chuanyang.driverh.mainmy.CarIdentifyActivity;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PermissoinUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.camera.DialogPhotoChooseView;
import com.chuanyang.driverh.utils.camera.DropDownList;
import com.chuanyang.driverh.utils.camera.SelectPictureListener;
import com.chuanyang.driverh.utils.camera.TakePictureListener;
import com.chuanyang.driverh.utils.eventbus.ImageEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class IdentifyCarPhotoFragment extends BaseFragment {
    private ImageView iv_photo_drivercard;
    private ImageView iv_photo_carddriver;
    private ImageView iv_photo_identity1;
    private ImageView iv_photo_identity2;
    private ImageView iv_photo_insurance1;
    private ImageView iv_photo_insurance2;
    private ImageView iv_photo_car1;
    private ImageView iv_photo_car2;
    private ImageView iv_photo_car3;
    private TextView tv_compete;
//    private int currentIndex=-1;
    private String[] imagepaths;

    private IdentifyCarBean identifyCarBean;

    @Override
    protected int getLayoutResId() {
        return R.layout.my_frag_identifyphoto;
    }

    @Override
    protected void initView(View view) {
        iv_photo_drivercard = (ImageView) rootView.findViewById(R.id.iv_photo_drivercard);
        iv_photo_carddriver = (ImageView) rootView.findViewById(R.id.iv_photo_carddriver);
        iv_photo_identity1 = (ImageView) rootView.findViewById(R.id.iv_photo_identity1);
        iv_photo_identity2 = (ImageView) rootView.findViewById(R.id.iv_photo_identity2);
        iv_photo_insurance1 = (ImageView) rootView.findViewById(R.id.iv_photo_insurance1);
        iv_photo_insurance2 = (ImageView) rootView.findViewById(R.id.iv_photo_insurance2);
        iv_photo_car1 = (ImageView) rootView.findViewById(R.id.iv_photo_car1);
        iv_photo_car2 = (ImageView) rootView.findViewById(R.id.iv_photo_car2);
        iv_photo_car3 = (ImageView) rootView.findViewById(R.id.iv_photo_car3);
        tv_compete = (TextView) rootView.findViewById(R.id.tv_identifyphoto_compete);


    }

    @Override
    protected void initData() {
        imagepaths = new String[9];
        if (getActivity() instanceof CarIdentifyActivity) {
            identifyCarBean = ((CarIdentifyActivity) getActivity()).getIdentifyCarBean();

        } else {
//            identifyCarBean = ((CarPersonIdentifyActivity) getActivity()).getIdentifyCarBean();


        }
    }

    @Override
    protected void initListener() {
        iv_photo_drivercard.setOnClickListener(this);
        iv_photo_carddriver.setOnClickListener(this);
        iv_photo_identity1.setOnClickListener(this);
        iv_photo_identity1.setOnClickListener(this);
        iv_photo_identity2.setOnClickListener(this);
        iv_photo_insurance1.setOnClickListener(this);
        iv_photo_insurance2.setOnClickListener(this);
        iv_photo_car1.setOnClickListener(this);
        iv_photo_car2.setOnClickListener(this);
        iv_photo_car3.setOnClickListener(this);
        tv_compete.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_identifyphoto_compete:
                if (checkAll()) {
                    setEntity();
                    carIdentify();
                }
                break;
            case R.id.iv_photo_drivercard:
                ( (CarIdentifyActivity)  getActivity()).setCurrentIndex(0);
                checkPermission();
                break;
            case R.id.iv_photo_carddriver:
                ( (CarIdentifyActivity)  getActivity()).setCurrentIndex(1);
                checkPermission();
                break;
            case R.id.iv_photo_identity1:
                ( (CarIdentifyActivity)  getActivity()).setCurrentIndex(2);
                checkPermission();
                break;
            case R.id.iv_photo_identity2:
                ( (CarIdentifyActivity)  getActivity()).setCurrentIndex(3);
                checkPermission();
                break;
            case R.id.iv_photo_insurance1:
                ( (CarIdentifyActivity)  getActivity()).setCurrentIndex(4);
                checkPermission();
                break;
            case R.id.iv_photo_insurance2:
                ( (CarIdentifyActivity)  getActivity()).setCurrentIndex(5);
                checkPermission();
                break;
            case R.id.iv_photo_car1:
                ( (CarIdentifyActivity)  getActivity()).setCurrentIndex(6);
                checkPermission();
                break;
            case R.id.iv_photo_car2:
                ( (CarIdentifyActivity)  getActivity()).setCurrentIndex(7);
                checkPermission();
                break;
            case R.id.iv_photo_car3:
                ( (CarIdentifyActivity)  getActivity()).setCurrentIndex(8);
                checkPermission();
                break;
        }
    }


    private void checkPermission() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        if (PermissoinUtil.checkAndRequestPermissions(getActivity(),perms)){
            DialogPhotoChooseView mChooseView = new DialogPhotoChooseView(getActivity()).setCameraListener(new TakePictureListener(getActivity()))
                    .setAlbumListener(new SelectPictureListener(getActivity()));
            DropDownList.showDialog(getActivity(), mChooseView);

        }else{
            PrintUtil.toast(getActivity(),"请给应用相应的权限");
        }
    }



    public void onEventMainThread(ImageEvent event) {
        if (event != null) {
            switch (event.getImageIndex())
            {
                case 0:
                    BitmapUtil.showImgForNetPath( event.getImagePath(),iv_photo_drivercard);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 1:
                    BitmapUtil.showImgForNetPath( event.getImagePath(), iv_photo_carddriver);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 2:
                    BitmapUtil.showImgForNetPath( event.getImagePath(), iv_photo_identity1);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 3:
                    BitmapUtil.showImgForNetPath( event.getImagePath(), iv_photo_identity2);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 4:
                    BitmapUtil.showImgForNetPath( event.getImagePath(), iv_photo_insurance1);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 5:
                    BitmapUtil.showImgForNetPath( event.getImagePath(),iv_photo_insurance2);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 6:
                    BitmapUtil.showImgForNetPath( event.getImagePath(),iv_photo_car1);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 7:
                    BitmapUtil.showImgForNetPath( event.getImagePath(),iv_photo_car2);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
                case 8:
                    BitmapUtil.showImgForNetPath( event.getImagePath(), iv_photo_car3);
                    imagepaths[event.getImageIndex()] = event.getImagePath();
                    break;
            }
        }
    }


    private boolean checkAll() {
        if (TextUtils.isEmpty(imagepaths[0])) {
            PrintUtil.toast(getActivity(),"请上传驾驶证图片");
            return false;
        } else if (TextUtils.isEmpty(imagepaths[1])) {
            PrintUtil.toast(getActivity(),"请上传行驶证图片");
            return false;
        } else if (TextUtils.isEmpty(imagepaths[2]) || TextUtils.isEmpty(imagepaths[3])) {
            PrintUtil.toast(getActivity(),"请上传身份证图片");
            return false;
        } else if (TextUtils.isEmpty(imagepaths[4]) || TextUtils.isEmpty(imagepaths[5])) {
            PrintUtil.toast(getActivity(),"请上传强交车险图片");
            return false;
        } else if (TextUtils.isEmpty(imagepaths[6]) || TextUtils.isEmpty(imagepaths[7]) || TextUtils.isEmpty(imagepaths[8])) {
            PrintUtil.toast(getActivity(),"请上传车辆照片");
            return false;
        }
        return true;
    }

    private void setEntity() {
        identifyCarBean.setDriverCardPt(imagepaths[0]);
        identifyCarBean.setCarDirverPt(imagepaths[1]);
        identifyCarBean.setIdentityPt1(imagepaths[2]);
        identifyCarBean.setIdentityPt2(imagepaths[3]);
        identifyCarBean.setInsurancePt1(imagepaths[4]);
        identifyCarBean.setInsurancePt2(imagepaths[5]);
        identifyCarBean.setCarPt1(imagepaths[6]);
        identifyCarBean.setCarPt2(imagepaths[7]);
        identifyCarBean.setCarPt3(imagepaths[8]);
    }

    private void carIdentify() {
        DialogUtil.showLoadingDialog(getActivity(),"认证...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("driverType", "2");
        params.put("carType", identifyCarBean.getCarType());
        params.put("plateNumber", identifyCarBean.getPlateNumber());
        params.put("carStandard", identifyCarBean.getCarStandard());
        params.put("capacity", identifyCarBean.getCapacity());
        params.put("actualCapacity", identifyCarBean.getActualCapacity());
        params.put("driverInsurance", identifyCarBean.getDriverInsurance());
        params.put("driverState", "2");
        params.put("CarMarkColor", identifyCarBean.getCarMarkColor());
        params.put("driverCardPt", identifyCarBean.getDriverCardPt());
        params.put("carDirverPt", identifyCarBean.getCarDirverPt());
        params.put("identityPt1", identifyCarBean.getIdentityPt1());
        params.put("identityPt2", identifyCarBean.getIdentityPt2());
        params.put("insurancePt1", identifyCarBean.getInsurancePt1());
        params.put("insurancePt2", identifyCarBean.getInsurancePt2());
        params.put("carPt1", identifyCarBean.getCarPt1());
        params.put("carPt2", identifyCarBean.getCarPt2());
        params.put("carPt3", identifyCarBean.getCarPt3());

        MyHttpUtil.addCarInfo(getActivity(),params, new MyCallback<Result>(getActivity(),new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(getActivity(), "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    PrintUtil.toast(getActivity(),"认证信息上传成功，请耐心等候");
                    BaseApplication.getInstance().getDriverInfoFromNet();
                    getActivity().onBackPressed();
                }else{
                    PrintUtil.toast(getActivity(),result.getMsg());
                }
            }
        });

    }

}
